<?php
/**
 * 获取文章的用户名
 * @param array $a 传入文章
 * @return string
 */
function qqq_art_admin($a)
{
    $userid = $a['adminuid'];
    $admininfo = $query = M()->one_assoc("SELECT username,nickname FROM " . tableex('admin') . " where id=?",array($userid));
    if (empty($admininfo)) {
        $admininfo['username'] = '';
        $admininfo['nickname'] = '';
    }

    return $admininfo['nickname'];
}

/**
 * 获取文章的多图
 * @param string $art_pics_value 文章中多图字段的值
 * @param string $liTpl
 * @return string
 * @internal param $article
 */
function qqq_art_imgs($art_pics_value, $liTpl = "<img src='{img}'>")
{
    $pics = json_decode($art_pics_value, 1);
    $imgs = "";
    foreach ($pics as $pic) {
        $imgs .= str_replace("{img}", $pic, $liTpl);
    }
    return $imgs;
}

/**
 * 获取文章内容中的图片
 * @param string $content 文章内容
 * @param int $count 要返回图片的数量，默认全部获取，不够时，返回全部图片
 * @return array 空数组 或 包含图片url的数组
 */
function qqq_art_content_imgs($content, $count = 0)
{
    $pattern = "/<img.*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.bmp|\.jpeg]))[\'|\"].*?[\/]?>/i";
    preg_match_all($pattern, $content, $match);
    $imgs = array();
    $imgCount = count($match[1]);
    if ($imgCount > 0) {
        //如果设定了获取数量，则返回指定数量的图片
        if ($count > 0) {
            $imgCount = $imgCount > $count ? $count : $imgCount;
        }

        for ($i = 0; $i < $imgCount; $i++) {
            $imgs[] = $match[1][$i];
        }
    }
    return $imgs;
}

/** 对ucms插入文章的补充
 * @param $cid
 * @param $insertArray
 * @return bool|int
 */
function qqq_add_art($cid, $insertArray)
{
    $article = array();
    $article['cid'] = $cid;
    foreach ($insertArray as $k => $v) {
        $article[$k] = $v;
    }

    $id = uuu_ainsert($article);
    if (is_numeric($id)) {
        return $id;
    }
    return false;
}

/** 对ucms修改文章的补充
 * @param $cid
 * @param $id
 * @param $editArray
 * @return bool|string
 */
function qqq_edit_art($cid, $id, $editArray)
{
    $article = array();
    $article['cid'] = $cid;
    $article['id'] = $id;
    foreach ($editArray as $k => $v) {
        if ($k != 'sql') {
            $article[$k] = dbstr($v);
        } else {
            $article[$k] = $v;
        }
    }

//    var_dump($article);
    return uuu_aedit($article);
}

/** 对ucms修改文章的补充，通过where查找并编辑
 * @param $cid
 * @param $where
 * @param $editArray
 * @return bool|string
 */
function qqq_edit_art_where($cid, $where, $editArray)
{
    $article = array();
    $article['cid'] = $cid;
    $a = qqq_select_art($cid, $where, false);
    if ($a) {
        return qqq_edit_art($cid, $a['id'], $editArray);
    } else {
        return false;
    }
}

//访问量+1
function qqq_click_art($cid, $id, $columnName = 'click', $clickNum = "+1")
{
    qqq_edit_art($cid, $id, array('sql' => $columnName . '=' . $columnName . $clickNum));
}

/** 对ucms删除文章的补充，通过where查找并删除
 * @param $cid
 * @param $where
 * @return bool|string
 */
function qqq_del_art_where($cid, $where)
{
    $article = array();
    $article['cid'] = $cid;
    $a = qqq_select_art($cid, $where);
    if ($a) {
        return uuu_adel($cid, $a['id']);
    } else {
        return false;
    }
}


/**
 * 查找一篇文章并返回
 * @param $cid
 * @param $whereArray
 * @param bool,int $ison 1,0  false表示不支持此字段
 * @return array|bool
 */
function qqq_select_art($cid, $whereArray, $ison = false)
{
    $set = array();
    $set['cid'] = $cid;
    $set['where'] = array();
    if ($ison !== false) {
        $set['where']['ison'] = $ison;
    }

    foreach ($whereArray as $k => $v) {
        $set['where'][$k] = dbstr($v);
    }

    $a = uuu_art($set);

    if ($a) {
        return $a;
    } else {
        return false;
    }
}

/**
 * 查找文章列表并返回
 * @param $cid
 * @param array $whereArray 填写order表示排序
 * @param bool,int $ison 1,0  false表示不支持此字段
 * @return array
 */
function qqq_select_artlist($cid, $whereArray, $ison = false)
{
    $set = array();
    $set['cid'] = $cid;
    $set['where'] = array();
    if ($ison !== false) {
        $set['where']['ison'] = $ison;
    }
    foreach ($whereArray as $k => $v) {
        if ($k == "order" || $k == "pagesize" ||  $k == "page") {
            $set[$k] = $v;
        } else {
            $set['where'][$k] = $v;
        }
    }

    $a = uuu_alist($set);

    if ($a && $a['list']) {
        return $a['list'];
    } else {
        return array();
    }
}


//获取后台登录用户名
function qqq_backadmin_name()
{
    if (isset($_COOKIE['admin_' . cookiehash]) && !empty($_COOKIE['admin_' . cookiehash])) {
        $username = trim($_COOKIE['admin_' . cookiehash]);
        if (!preg_match("/^[a-zA-Z0-9_]+$/", $username)) {
            return false;
        }
        return trim($username);
    }
    return false;
}

//获取后台登录用户昵称
function qqq_backadmin_nick()
{
    if (isset($_COOKIE['nick_' . cookiehash]) && !empty($_COOKIE['nick_' . cookiehash])) {
        $nick = trim($_COOKIE['nick_' . cookiehash]);
        $nick = htmlspecialchars($nick);
        return $nick;
    }
    return "noname";
}

/**
 * 生成登录后跳转回原页面的链接
 * @param string $backUrl 登录后需要跳转回的页面
 * @return string
 */
function qqq_get_login_then_goto_url($backUrl = "/")
{
    return "/" . AdminUrl . "/login.php?goto=" . urlsafe_b64encode($backUrl);
}

//返回站内链接
function qqq_site_url($uri)
{
    $uri = ltrim($uri, '/');
    if (UrlRewrite) {
        $thisurl = SystemDir . $uri;
        $thisurl = str_replace("//", "/", $thisurl);
        return $thisurl;
    } else {
        return SystemDir . IndexFile . '/' . $uri;
    }
}

/**
 * 获取主页链接
 * @return mixed|string
 */
function qqq_homeurl()
{
    if (isset($GLOBALS['homeurl'])) {
        return $GLOBALS['homeurl'];
    }
    if (!UrlRewrite) {
        $indexfile = IndexFile;
    } else {
        $indexfile = '';
    }
    $domains = explode(';', SystemDomain);
    foreach ($domains as $domain) {
        $domain = trim($domain);
        if ($domain == server_name()) {
            $GLOBALS['homeurl'] = SystemDir . $indexfile;
            return $GLOBALS['homeurl'];
        }
    }
    if (SystemDomain == '') {
        $GLOBALS['homeurl'] = SystemDir . $indexfile;
        return $GLOBALS['homeurl'];
    }
    $GLOBALS['homeurl'] = '//' . $domains[0] . server_port() . SystemDir . $indexfile;
    return $GLOBALS['homeurl'];
}

/**
 * 栏目单个属性（变量）
 * @param $strname string 变量名
 * @param $cid int 栏目cid
 * @return bool|mixed
 */
function qqq_one_cfg($strname,$cid){
    return uuu_str($strname, $cid, 1);
}

/**
 * 栏目的所有属性
 * @param $cid int 栏目cid
 * @return array
 */
function qqq_all_cfg($cid){
    return uuu_all_cfg($cid);
}

